package com.kethink;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description create by xuxiaojun
 * date: 2018/12/26
 * Time: 18:26
 */
public class LeeCode_830 {

    public static List<List<Integer>> largeGroupPositions(String S) {

        char[] chars = S.toCharArray();
        //用来判断i自增的时候会不会溢出下标
        boolean flag = false;
        List<List<Integer>> result = new ArrayList();
        for(int i = 0 ;i < chars.length-2 ; i++){
            int begin = -1;
            int end = 0;
            //如果有三个连续的字符相等
            List<Integer> list = null;
            while (chars[i] == chars[i+1] && chars[i+1] == chars[i+2]){
                flag = false;
                if(begin == -1){
                    list = new ArrayList(2);
                    begin = i;
                }
                //i不溢出
                if(i < chars.length - 3){
                    i++;
                    flag = true;
                }else {
                    break;
                }
            }
            if(begin > -1 ){
                if(flag){
                    end = i + 1;
                }else {
                    //如果i溢出了
                    end = i + 2;
                }
                list.add(begin);
                list.add(end);
                result.add(list);
            }
        }
        return result;
    }


    public static void main(String[] args){
        String S = "aaa";
//        String S = "abcdddeeeeaabbbcd";
        //  [[3,5],[6,9],[12,14]]
        System.out.println(largeGroupPositions(S));
    }
}
